package com.cy.ppj.web.admin.city;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.city.CityAddAO;
import com.cy.ppj.model.domian.city.City;
import com.cy.ppj.model.enums.CityStatus;
import com.cy.ppj.model.query.CityListAdminQuery;
import com.cy.ppj.model.vo.CityListAdminVO;
import com.cy.ppj.service.city.CityService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 城市管理
 * @author: tanyilin
 * @create: 2018年8月14日 上午11:29:11
 */
@Slf4j
@RestController
@RequestMapping("/admin/city")
@Api(tags = "后台-城市管理")
public class AdminCityController extends BaseController{

	@Autowired
	private CityService cityService;
	
	@ApiOperation("后台-城市列表")
	@GetMapping("/list")
	public PageBean<CityListAdminVO> list(CityListAdminQuery query) {
		PageBean<CityListAdminVO> pageBean = cityService.selectCityListAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("新增城市")
	@PostMapping("/add")
	public Result add(@Validated CityAddAO cityAddAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		City city = new City();
		BeanUtils.copyProperties(cityAddAO, city);
		cityService.addCity(city);
		return ok();
	}
	
	@ApiOperation("更改状态")
	@PostMapping("/update/status")
	public Result updateStatus(@ApiParam(value = "城市ID", example = "0", required = true) @RequestParam Long cityId,
			@ApiParam(value = "状态", required = true) @RequestParam CityStatus status) {
		cityService.updateStatus(cityId, status);
		return ok();
	}
	
	@ApiOperation("获取全部的城市列表")
    @GetMapping("/all/lists")
    public Result cityList() {
    	List<City> list = cityService.getAllCityInCache();
    	return ok("", list);
    }
}
